/**
 * Created by yangfan on 2016/10/18.
 */
/**
 * Created by yangfan on 2016/10/13.
 */
define(['config/appregister',
    "i18n",
    "business/personMine/services/httpServices",
    "business/services/sendMessage",
    "business/services/components"],function(app,i18n,httpService){
    app.controller('myMessageCtrl', function($scope,$state,$q) {
        $scope.i18n = i18n;
        $scope.everPageShow = [30,50,100];
        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.tableTitle = ["主题","评论时间"];
        $scope.titleNavation = [
            {title:"通知",type:"myComment"},
            {title:"私信",type:"commented"},
        ];
        $scope.isShowCurrent = [];
        $scope.isShowCurrent[0] = true;
		$scope.isShow = 0;
        //待切换加载数据
        $scope.checkTitle = function(type,index){
        	$scope.isShow = index;
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10;
            $scope.paginationConf.itemsPerPage = 10;
            $scope.paginationConf.numberOfPages = Math.ceil($scope.totalItem/$scope.listParams.pageSize);
            $scope.isShowCurrent = [];
            $scope.isShowCurrent[index] = true;
            if(index == 0){
                $scope.titleNavation.type = "myComment";
                getNotifyList();
            }else if(index ==1){
                $scope.titleNavation.type = "commented";
                getMessageList();
            }
        };
        function getCurList(){
            if($scope.isShowCurrent[0] == true){
                getNotifyList();
            }
            if($scope.isShowCurrent[1] == true){
                getMessageList();
            }
        }
		$scope.dialogModel = {
            content:"您确定要删除该消息吗？",
            isShow:false,
            confirm:function(){
                deleteNotify();
            }
        }
		

        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";
        $scope.listParams = {
            pageNo:1,
            pageSize:10
        };

        function getPage (){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength: 5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    getCurList();
                }
            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        }



        
        //获取通知
        function getNotifyList(){
        	var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    all:'false'
                },
                uri:"getList"
            };
            httpService.notifyServices(req).success(function(response){
                $scope.$apply(function(){
                	$scope.noContent = "您暂时没有通知";
                    $scope.notify = response.data.list;
                    $scope.totalItem = response.data.Pagination.totalCount ;
                    if($scope.listParams.pageNo == 1){
                        getPage();
                    }
                })
            }).error(function(){
                console.log("访问出错");
            })
        }
		
		//删除通知
		function deleteNotify(){
			
			if($scope.willDel.notifyId){//删除私信
				var req = {
					type:"DELETE",
					data:{
	                    notifyId:$scope.willDel.notifyId
	                },
	                uri:"del"
				}
				httpService.notifyServices(req).success(function(response){
	                $scope.$apply(function(){
	                    alert(response.msg);
	                })
	            }).error(function(){
	                console.log("访问出错");
	            })
			}else if($scope.willDel.messageId){//删除消息
				var req = {
	                type:'DELETE',
	                data:{
	                	messageId:$scope.willDel.messageId
	                },
	                uri:'removeMessage'
	            };
	            httpService.messageServices(req).success(function(response){
	                $scope.$apply(function(){
	                    alert(response.msg);
	                })
	            }).error(function(){
	                console.log("访问出错");
	            })
			}
			
            
		}
		$scope.del = function(item){
			$scope.willDel = item;
			$scope.dialogModel.isShow = true;
		};
		//获取私信
		function getMessageList(){
        	var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize
                },
                uri:"getMessageList"
            };
            httpService.messageServices(req).success(function(response){
                $scope.$apply(function(){
                	$scope.noContent = "您暂时没有私信";
                    $scope.message = response.data.list;
                    $scope.totalItem = response.data.Pagination.totalCount ;
                    if($scope.listParams.pageNo == 1){
                        getPage();
                    }
                })
            }).error(function(){
                console.log("访问出错");
            })
        }
        getCurList();

        //清空私信
        function clearMessage(){
            var req = {
                type:"DELETE",
                uri:"messageServices"
            };
            httpService.messageServices(req).success(function(response){
                $scope.apply(function(){
                    $scope.clearAllWindow = false;
                })
                getCurList()
            }).error(function(){})
        }
        function clearNotify(){
            var req = {
                type:"DELETE",
                uri:"clearNotify"
            };
            httpService.notifyServices(req).success(function(response){
                $scope.apply(function(){
                    $scope.clearAllWindow = false;
                })
                getCurList()
            }).error(function(){})
        }
        function sendMessage (item){
            console.log($scope.sendMessageModel.byUserId);
            var deffer = $q.defer();
            var req = {
                type:"POST",
                uri:"addMessage",
                data:{
                    byUserId: $scope.sendMessageModel.byUserId,
                    content: $scope.sendMessageModel.content
                }
            };
            httpService.messageServices(req).success(function(response){
                deffer.resolve();
            }).error(function(){
                deffer.reject();
            })
            return deffer.promise;
        }
        $scope.clearAllWindow = {
            content:"您确定要清空所有记录吗？",
            isShow:false,
            confirm:function(){
                if($scope.isShowCurrent[0] == true){
                    clearMessage();
                }
                if($scope.isShowCurrent[1] == true){
                    clearNotify();
                }
            }
        }
        $scope.clearRecord = function (){
            $scope.clearAllWindow.isShow = true;
        };
        $scope.sendMessageModel = {
            content:"",
            name:"",
            byUserId:"",
            isShow:false,
            showSuccessNotice:false,
            showErrorNotice:false,
            confirm:function(){
              return   sendMessage()
            }
        }
        $scope.sendMessage = function(item){
            $scope.sendMessageModel.isShow = true ;
            $scope.sendMessageModel.byUserId = item.byUserId;
            $scope.sendMessageModel.name = item.name;

        };

        $scope.readMessage = function(item){
            var req = {

            }
            if($scope.titleNavation.type = "myComment"){
                var req = {
                    type:"GET",
                    uri:"getInfo",
                    data:{
                        notifyId:item.messageId
                    }
                };
                httpService.notifyServices(req).success(function(response){

                        if(item.userType == 1){
                            $state.go('othersMainPage',{userId:item.byUserId})
                        }else{
                            $state.go('outIndexCtrl',{companyId:item.byUserId})
                        }
                })
            }else if($scope.titleNavation.type = "myComment"){
                var req = {
                    type:"POST",
                    uri:"readMessage",
                    data:{
                        messageId:item.messageId
                    }
                };
                httpService.messageServices(req).success(function(response){

                })
            }

        }



        //改变每页预览条数
        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = Math.ceil($scope.totalItem/$scope.listParams.pageSize);
            getCurList();
        }
    })
});
